package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.LoanEditStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.DpBaseDataDictionaryMapper;
import com.zjdiepu.www.dao.his.HisYwBusinessLoanMapper;
import com.zjdiepu.www.dao.yw.YwBusinessExtrasMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanEditMapper;
import com.zjdiepu.www.dao.yw.YwBusinessLoanMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanEditService;
import com.zjdiepu.www.ibiz.zpp.SystemService;
import com.zjdiepu.www.model.his.dto.HisYwBusinessLoan;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessExtras;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoanEdit;
import com.zjdiepu.www.model.yw.ext.YwBusinessLoanEditExt;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * yw_贷款信息修改表Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年10月16日 15时43分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwBusinessLoanEditServiceImpl extends BaseService implements IYwBusinessLoanEditService {
	public static final Logger logger = LoggerFactory.getLogger(YwBusinessLoanEditServiceImpl.class);

	@Autowired
	private YwBusinessLoanEditMapper ywBusinessLoanEditMapper;
	@Autowired
	private YwBusinessLoanMapper ywBusinessLoanMapper;
	@Autowired
	private HisYwBusinessLoanMapper hisYwBusinessLoanMapper;
	@Autowired
	private YwBusinessExtrasMapper ywBusinessExtrasMapper;
	@Resource
	private SystemService systemService;
	@Autowired
	private DpBaseDataDictionaryMapper dpBaseDataDictionaryMapper;
	
	@Override
	public int countMultiByExtForUpdateHistory(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywBusinessLoanEditMapper.countMultiByExtForUpdateHistory(ywBusinessQueryRequest);
	}
	
	@Override
	public List<YwBusiness> selectMultiByExtForUpdateHistory(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywBusinessLoanEditMapper.selectMultiByExtForUpdateHistory(ywBusinessQueryRequest);
	}
	
	@Override
	public void saveUpdateAndDelete(YwBusinessLoanEdit ywBusinessLoanEdit, OperateType operType) {
		switch (operType) {
		case add:
			ywBusinessLoanEdit.initInsertData(ywBusinessLoanEdit.getModifier(), ywBusinessLoanEdit.getModifierName());
			ywBusinessLoanEditMapper.insert(ywBusinessLoanEdit);
			break;
		case edit:
			ywBusinessLoanEdit.initUpdateData(ywBusinessLoanEdit.getModifier(), ywBusinessLoanEdit.getModifierName());
			// 影响的记录数
			int updateCount = ywBusinessLoanEditMapper.updateByPrimaryKeySelective(ywBusinessLoanEdit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywBusinessLoanEdit.initDeleteData(ywBusinessLoanEdit.getModifier(), ywBusinessLoanEdit.getModifierName());
			// 影响的记录数
			int deleteCount = ywBusinessLoanEditMapper.updateByPrimaryKeySelective(ywBusinessLoanEdit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwBusinessLoanEdit> selectByExt(YwBusinessLoanEditExt ywBusinessLoanEditExt) {
		return ywBusinessLoanEditMapper.selectByExt(ywBusinessLoanEditExt);
	}
	
	@Override
	public int countByExt(YwBusinessLoanEditExt ywBusinessLoanEditExt){
		return ywBusinessLoanEditMapper.countByExt(ywBusinessLoanEditExt);
	}
	
	@Override
	public YwBusinessLoanEdit selectById(String id) {
		return ywBusinessLoanEditMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywBusinessLoanEditMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwBusinessLoanEdit ywBusinessLoanEdit) {
		return ywBusinessLoanEditMapper.countRefined(ywBusinessLoanEdit);
	}

	@Override
	public YwBusinessLoanEdit selectRefined(YwBusinessLoanEdit ywBusinessLoanEdit) {
		return ywBusinessLoanEditMapper.selectRefined(ywBusinessLoanEdit);
	}

	@Override
	public boolean repeat(YwBusinessLoanEdit ywBusinessLoanEdit) {
		int count = countRefined(ywBusinessLoanEdit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywBusinessLoanEdit.getId())) {
			return true;
		}
		YwBusinessLoanEdit dbItem = selectRefined(ywBusinessLoanEdit);
		if(null == dbItem) {
			return false;
		}
		return !ywBusinessLoanEdit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywBusinessLoanEditMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywBusinessLoanEditMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<YwBusiness> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		return ywBusinessLoanEditMapper.selectMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public void saveOrUpdateLoan(YwBusinessLoanEdit ywBusinessLoanEdit,
			AbstractSysUser currentUser) {
		String userId=currentUser.getId();
		String currentName=	currentUser.getName();	
		String loanId=null;
		//新增/修改到his_历史贷款信息修改表
		String hisId="";
		String ywHisLoanId=ywBusinessLoanEdit.getHisLoanId();
		YwBusinessLoan ywBusinessLoan = new YwBusinessLoan();
		HisYwBusinessLoan hisYwBusinessLoan=new HisYwBusinessLoan();
		ywBusinessLoan.setDpStatus(DpStatus.NORMAL.getCode());
		ywBusinessLoan.setBusiId(ywBusinessLoanEdit.getBusiId());
		ywBusinessLoan=ywBusinessLoanMapper.selectRefined(ywBusinessLoan);
		loanId=ywBusinessLoan.getId();
		if(ywBusinessLoan!=null){
			if(StringUtils.isNotBlank(ywHisLoanId)){
				BeanUtils.copyProperties(ywBusinessLoan,hisYwBusinessLoan);
				hisYwBusinessLoan.setId(ywHisLoanId);
				hisId=ywHisLoanId;
				hisYwBusinessLoan.initUpdateData(userId, currentName);
				hisYwBusinessLoanMapper.updateByPrimaryKeySelective(hisYwBusinessLoan);
			}else{
				BeanUtils.copyProperties(ywBusinessLoan,hisYwBusinessLoan);
				hisYwBusinessLoan.setLoanId(ywBusinessLoan.getId());
				hisYwBusinessLoan.initInsertData(userId, currentName);
				hisId=hisYwBusinessLoan.getId();
				hisYwBusinessLoanMapper.insert(hisYwBusinessLoan);
			}
			
			//将车贷信息（新）中的值填入 yw_贷款信息修改表，申请状态 为 已申请；
			String id = ywBusinessLoanEdit.getId();
			ywBusinessLoanEdit.setLoanId(loanId);
			ywBusinessLoanEdit.setHisLoanId(hisId);
			ywBusinessLoanEdit.setLoanEditStatus(LoanEditStatus.YI_SHEN_QING.getCode());
			if(StringUtils.isNotBlank(id)) {
				ywBusinessLoanEdit.initUpdateData(userId, currentName);
				ywBusinessLoanEditMapper.updateByPrimaryKeySelective(ywBusinessLoanEdit);
			} else {
				ywBusinessLoanEdit.initInsertData(userId, currentName);
				ywBusinessLoanEditMapper.insert(ywBusinessLoanEdit);
			}
			
			//往yw_业务信息_附加信息 插入一条 附加值名称 为‘贷款信息修改锁定’  的信息，值为 1 （如果已经存在，更新值为1）；
			YwBusinessExtras ywBusinessExtras=new YwBusinessExtras();
			ywBusinessExtras.setBusiId(ywBusinessLoanEdit.getBusiId());
			ywBusinessExtras.setCodeExtras("LoanEditLock");
			YwBusinessExtras ye=ywBusinessExtrasMapper.selectRefined(ywBusinessExtras);
			if(ye!=null){
				ye.setValueExtras("1");
				ye.setNameExtras("贷款信息修改锁定");
				ye.setCodeExtras("LoanEditLock");
				ye.setTypeExtras("LockStatus");
				ye.initUpdateData(userId, currentName);
				int updateCount1=ywBusinessExtrasMapper.updateByPrimaryKeySelective(ye);
				if(updateCount1 == 0){
					throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
				} else if(updateCount1 > 1){
					throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
				}
			}else{
				ywBusinessExtras.setValueExtras("1");
				ywBusinessExtras.setNameExtras("贷款信息修改锁定");
				ywBusinessExtras.setCodeExtras("LoanEditLock");
				ywBusinessExtras.setTypeExtras("LockStatus");
				ywBusinessExtras.initInsertData(userId, currentName);
				ywBusinessExtrasMapper.insert(ywBusinessExtras);
			}
		}
		systemService.diepuNewsHandle(currentUser, ModeIDType.SQXGDKXX.getCode(), "37", ywBusinessLoanEdit.getBusiId(), "", "");
	}
}
